package com.op.crm.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Encrypt {
	
	//默认加密key
	private static final String DEFAULT_KEY = "a1b2c3d4";
	
	//加密方式
	private static final String ENCRYPT_MD5 = "MD5";
	
	public static String toMD5(String msg) {
		String pwd = null;
		
		StringBuffer sb = new StringBuffer();
		sb.append(msg);
		sb.append(DEFAULT_KEY);
		
		try {
			MessageDigest msd = MessageDigest.getInstance(ENCRYPT_MD5);
			msd.update(sb.toString().getBytes());
			byte da[] = msd.digest();
            pwd = byte2hex(da);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		
		return pwd;
	}
	
	private static String byte2hex(byte h[]) {
        String hs = "";
        String stmp = "";
        for(int n = 0; n < h.length; n++)
        {
            stmp = Integer.toHexString(h[n] & 0xff);
            if(stmp.length() == 1)
                hs = hs + "0" + stmp;
            else
                hs = hs + stmp;
            if(n < h.length - 1)
                hs = hs + ":";
        }

        return hs.toUpperCase();
    }
}
